package com.java.st.lib.tools.http;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * Created by wd on 2016/6/28.
 */
@Slf4j
public class HttpStringResponse implements ResponseHandler<String> {

	@Override
	public String handleResponse(HttpResponse response) throws IOException {

		StatusLine statusLine = response.getStatusLine();
		

        HttpEntity entity = response.getEntity();
        if (statusLine.getStatusCode() >= 300) {
            log.info("请求被重定向，无法接受任何数据！{} ",EntityUtils.toString(response.getEntity(), "UTF-8"));
            return EntityUtils.toString(response.getEntity(), "UTF-8");
        }
        if (entity == null) {
        	log.error("Response contains no content");
        	return null;
           // throw new ClientProtocolException("Response contains no content");
        }
        log.info("HttpReponseString \r\n {} ",EntityUtils.toString(entity), "UTF-8");
        return EntityUtils.toString(entity);
	}
}
